import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Sample data
time = list(range(1000))
pose = [i * 0.5 for i in time] # Example time series data for pose
# Action data
actions = ['walk', 'run', 'idle'] * (len(time) // 3 + 1)
action_times = [(i * 10, (i + 1) * 10) for i in range(len(time) // 10)]
action_colors = {
'walk': 'blue',
'run': 'red',
'idle': 'green'
}
# Create subplots
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.1,
subplot_titles=("Time Series of Pose", "Action Sequence"))
# Add time series to the top subplot
fig.add_trace(go.Scatter(x=time, y=pose, mode='lines', name='Pose'), row=1, col=1)
# Add dummy traces for legend
for action in action_colors.keys():
fig.add_trace(go.Scatter(
x=[None], y=[None], mode='markers',
marker=dict(size=10, color=action_colors[action]),
name=action
), row=2, col=1)
# Add action rectangles to the bottom subplot
for action, (start, end) in zip(actions, action_times):
fig.add_trace(go.Scatter(
x=[start, end, end, start, start],
y=[0, 0, 1, 1, 0],
fill="toself",
fillcolor=action_colors[action],
line=dict(color="rgba(0,0,0,0)"),
showlegend=False,
hoverinfo='none'
), row=2, col=1)
# Update layout for better visualization
fig.update_layout(height=600, width=1000, title_text="Ethogram Visualization",
xaxis2=dict(rangeslider=dict(visible=True),
type="linear"))
fig.update_yaxes(visible=False, row=2, col=1) # Hide y-axis for the action subplot
fig.update_xaxes(title_text="Time", row=2, col=1)
fig.update_yaxes(title_text="Pose", row=1, col=1)
fig.show()